package com.jrh.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jrh.biz.siteMsg.SiteMsgService;
import com.jrh.common.exception.DataNotFoundException;
import com.jrh.domain.entity.SiteMsg;

/**
 * 后台站内短信息控制器
 * 
 * @author yout 2012-7-22 上午11:16:12
 */
@Controller
@RequestMapping("/admin/siteMsg")
public class AdminSiteMsgController {

    private static final Integer pageSize = 5;

    @Autowired
    private SiteMsgService       siteMsgService;

    @RequestMapping(method = RequestMethod.GET, value = "/send")
    public String send() {
        return "admin/siteMsg/sendMsg";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/send")
    public String doSend(@ModelAttribute("siteMsg") SiteMsg siteMsg, Model model) {
        boolean isSuccess;
        try {
            // TODO 发送人LOGIN_ID未传递
            siteMsg.setSenderLoginId("mmyjse@gmail.com");
            isSuccess = siteMsgService.sendSiteMsg(siteMsg);
            if (isSuccess) {
                model.addAttribute("success", true);
                return "/admin/siteMsg/sendMsg";
            } else {
                return "/admin/error";
            }
        } catch (DataNotFoundException e) {
            return "/admin/error";
        }
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/delete/{id}")
    public String delete(@PathVariable Long id) {
        boolean isSuccess = siteMsgService.deleteSiteMsg(id);
        if (isSuccess) {
            return Boolean.TRUE.toString();
        } else {
            return Boolean.FALSE.toString();
        }
    }
}
